package com.ruoyi.internship.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.internship.dto.PositionDetailDTO;
import com.ruoyi.internship.vo.PositionList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.internship.domain.BolePostionApplication;
import com.ruoyi.internship.mapper.BolePositionMapper;
import com.ruoyi.internship.domain.BolePosition;
import com.ruoyi.internship.service.IBolePositionService;

/**
 * 职位信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-29
 */
@Service
public class BolePositionServiceImpl implements IBolePositionService 
{
    @Autowired
    private BolePositionMapper bolePositionMapper;

    @Override
    public PositionDetailDTO getPositionDetail(Long positionId, Long companyId) {
        return bolePositionMapper.selectPositionDetail(positionId, companyId);
    }

    @Override
    public List<PositionList> getPostionList(String keyword) {
        return bolePositionMapper.selectPositionList(keyword);
    }

    /**
     * 查询职位信息
     * 
     * @param positionId 职位信息主键
     * @return 职位信息
     */
    @Override
    public BolePosition selectBolePositionByPositionId(String positionId)
    {
        return bolePositionMapper.selectBolePositionByPositionId(positionId);
    }

    /**
     * 查询职位信息列表
     * 
     * @param bolePosition 职位信息
     * @return 职位信息
     */
    @Override
    public List<BolePosition> selectBolePositionList(BolePosition bolePosition)
    {
        return bolePositionMapper.selectBolePositionList(bolePosition);
    }

    /**
     * 新增职位信息
     * 
     * @param bolePosition 职位信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBolePosition(BolePosition bolePosition)
    {
        bolePosition.setCreateTime(DateUtils.getNowDate());
        int rows = bolePositionMapper.insertBolePosition(bolePosition);
        insertBolePostionApplication(bolePosition);
        return rows;
    }

    /**
     * 修改职位信息
     * 
     * @param bolePosition 职位信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBolePosition(BolePosition bolePosition)
    {
        bolePosition.setUpdateTime(DateUtils.getNowDate());
        bolePositionMapper.deleteBolePostionApplicationByPositionId(bolePosition.getPositionId());
        insertBolePostionApplication(bolePosition);
        return bolePositionMapper.updateBolePosition(bolePosition);
    }

    /**
     * 批量删除职位信息
     * 
     * @param positionIds 需要删除的职位信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBolePositionByPositionIds(String[] positionIds)
    {
        bolePositionMapper.deleteBolePostionApplicationByPositionIds(positionIds);
        return bolePositionMapper.deleteBolePositionByPositionIds(positionIds);
    }

    /**
     * 删除职位信息信息
     * 
     * @param positionId 职位信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBolePositionByPositionId(String positionId)
    {
        bolePositionMapper.deleteBolePostionApplicationByPositionId(positionId);
        return bolePositionMapper.deleteBolePositionByPositionId(positionId);
    }

    /**
     * 新增职位申请记录信息
     * 
     * @param bolePosition 职位信息对象
     */
    public void insertBolePostionApplication(BolePosition bolePosition)
    {
        List<BolePostionApplication> bolePostionApplicationList = bolePosition.getBolePostionApplicationList();
        String positionId = bolePosition.getPositionId();
        if (StringUtils.isNotNull(bolePostionApplicationList))
        {
            List<BolePostionApplication> list = new ArrayList<BolePostionApplication>();
            for (BolePostionApplication bolePostionApplication : bolePostionApplicationList)
            {
                bolePostionApplication.setPositionId(positionId);
                list.add(bolePostionApplication);
            }
            if (list.size() > 0)
            {
                bolePositionMapper.batchBolePostionApplication(list);
            }
        }
    }
}
